package com.example.recyclerviewdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolde> {
    private List<ContactModel> dataList;
    private Context context;

    public void setDataList(List<ContactModel> dataList) {
        this.dataList = dataList;
    }

    public MyAdapter(Context context) {
        this.context = context;
    }

    //创建VIewHolder对象，并且传入条目布局控件对象
    @NonNull
    @Override
    public MyHolde onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyHolde(LayoutInflater.from(context).inflate(R.layout.contact_text,null));
    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter.MyHolde holder, int position) {
         holder.contactName.setText(dataList.get(position).getContactName());
         holder.contactIcon.setImageResource(dataList.get(position).getContactIconTd());
    }

    @Override
    public int getItemCount() {
        return null !=dataList?dataList.size():0;
    }

    class MyHolde extends RecyclerView.ViewHolder{
        TextView contactName;
        ImageView contactIcon;
        public MyHolde(View itemView){
            super(itemView);
            contactName=itemView.findViewById(R.id.list_item_contact_name);
            contactIcon=itemView.findViewById(R.id.list_item_contact_icon);

        }
    }
}
